import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.regex.*;
import java.util.stream.*;
import java.text.*;

public class PriceCalculator {
    public static void main(String[] args) {
        String filePath = "prices.txt";

        try {
            List<String> lines = Files.readAllLines(Paths.get(filePath));
            List<Double> prices = new ArrayList<>();
            for (String line : lines) {
                Matcher matcher = Pattern.compile("(\\d+)元").matcher(line);
                if (matcher.find()) {
                    prices.add(Double.parseDouble(matcher.group(1)));
                }
            }
            double averagePrice = prices.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
            NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
            String averagePriceStr = numberFormat.format(averagePrice) + "元/台";
            try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(filePath), StandardOpenOption.APPEND)) {
                writer.newLine();
                writer.write("平均价格:" + averagePriceStr);
            }
            System.out.println("平均价格已计算并追加到文件中: " + averagePriceStr);
        } catch (IOException e) {
            System.err.println("文件操作失败: " + e.getMessage());
        }
    }
}